﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;
using PPAP.Model;

namespace PPAP.Web.Part
{
    public partial class PPAPPrint : PageBase
    {
        public string RID { get; set; }
        PPARBO ppbo = new PPARBO();
        PARTBO pabo = new PARTBO();
        DictionaryBO dbo = new DictionaryBO();
        PlantBO plbo = new PlantBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            PPARBO bo = new PPARBO();
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["RID"] != null)
                {
                    RID = Request.QueryString["RID"].ToString();
                    LoadPlant(Request.QueryString["RID"].ToString());
                    LoadItem();
                    DataSet ds = bo.GetListForPPARid(RID);
                    DataRow dr = null;
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        dr = ds.Tables[0].Rows[0];
                        lblColor.Text = dr["color"].ToString();
                        lblPartName.Text = dr["bdntpartname"].ToString();
                        lblPartNo.Text = dr["bdntpartno"].ToString();
                        lblPPARdate.Text = dr["ppardate"].ToString() == "" ? "" : DateTime.Parse(dr["ppardate"].ToString()).ToString("yyyy-MM-dd");
                        LblPPARNo.Text = dr["pparno"].ToString();
                        lblRevisionNo.Text = dr["bdntrevisionno"].ToString();
                        lblRevisionStatus.Text = dr["bdntrevisionstatus"].ToString();
                        lblStatus.Text = ShowAuditStatus(dr["status"].ToString(), dr["isinapp"].ToString());
                        lblSupplierCode.Text = dr["suppliercode"].ToString();
                        lblSupplierName.Text = dr["suppliername"].ToString();
                        lbContactPerson.Text = dr["firstname"].ToString() == "" ? "" : dr["firstname"].ToString() + " " + dr["name"].ToString();
                        lblPersonInformation.Text = dr["email"].ToString() == "" ? "" : dr["email"].ToString() + "/" + dr["telephone"].ToString();
                    }
                    LoadDate(Request.QueryString["RID"].ToString());
                }
            }
        }

        /// <summary>
        /// 加载数据字典
        /// </summary>
        private void LoadItem()
        {
            //ddlInitialSampling.Items.Clear();
            //ddlOtherSamples.Items.Clear();
            //DataSet dsInitial = dbo.GetDictionaryByParentid(2);
            //DataSet dsOther = dbo.GetDictionaryByParentid(3);
            //foreach (DataRow dr in dsInitial.Tables[0].Rows)
            //{
            //    ddlInitialSampling.Items.Add(new ListItem(dr["name"].ToString(), dr["code"].ToString()));
            //}
            //foreach (DataRow dr in dsOther.Tables[0].Rows)
            //{
            //    ddlOtherSamples.Items.Add(new ListItem(dr["name"].ToString(), dr["code"].ToString()));
            //}
        }

        /// <summary>
        /// 加载工厂
        /// </summary>
        /// <param name="pparid"></param>
        private void LoadPlant(string pparid)
        {
            //DataSet ds = ppbo.GetPlantListForPPARid(pparid);
            //if (ds.Tables[0].Rows.Count > 0)
            //{
            //    foreach (DataRow dr in ds.Tables[0].Rows)
            //    {
            //        ddlPlant.Items.Add(new ListItem(dr["plantname"].ToString(), dr["id"].ToString()));
            //    }
            //}
            //else
            //{
            //    ddlPlant.Items.Add(new ListItem("Not added plant information", "0"));
            //}
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="id"></param>
        private void LoadDate(string id)
        {
            PPAR p = ppbo.GetModel(int.Parse(id));
            PART r = pabo.GetModelForPPARid(int.Parse(id));
            if (p != null && r != null)
            {
                txtSupplierPartNo.Text = r.SUPPLIERPARTNO;
                txtSupplierPartName.Text = r.SUPPLIERPARTNAME;
                //ddlPlant.SelectedValue = r.PLANTID.ToString();
                Plant plant = plbo.GetModel(int.Parse(r.PLANTID == null ? "0" : r.PLANTID.ToString()));
                if (plant != null)
                    ddlPlant.Text = plant.PLANTNAME;
                //rboOtherSamples.Checked = r.REASONFORSAMPLING == "Other samples" ? true : false;
                //rboInitialSampling.Checked = r.REASONFORSAMPLING == "Initial sampling" ? true : false;
                //rboOtherSamples0.Checked = r.REASONFORSAMPLING == "Re-submission" ? true : false;
                lblReason.Text = r.REASONFORSAMPLING;
                if (r.REASONFORSAMPLING == "Other samples")
                    lblReason2.Text = r.OTHERSAMPLES;
                else if (r.REASONFORSAMPLING == "Initial sampling")
                    lblReason2.Text = r.INTTIALSAMPLING;
                else
                    lblReason2.Text = "";
                //ddlInitialSampling.Text = r.INTTIALSAMPLING;
                //ddlOtherSamples.Text = r.OTHERSAMPLES;
                txtQLevel.Text = p.QLEVEL == "" ? "001" : p.QLEVEL;
                ddlLevel.Text = p.ELEVEL;
                txtModificationScope.Text = r.MODIFICATIONSCOPE;
                txtRemarks.Text = r.REMARKS;
                txtECUHW.Text = r.ECUHWVERSIONNO;
                txtECUSW.Text = r.ECUSWVERSIONNO;
                string[] month = r.PRESSPARTMOP.Split('-');
                if (month.Length > 1)
                {
                    ddlYear.Text = month[0].ToString();
                    ddlMonth.Text = month[1].ToString();
                }
                txtPressPartPressCycle.Text = r.PRESSPARTCYCLE;
                txtPressPartMarkingNest.Text = r.PRESSPARTNEST;
                txtPressPartRevisionNumber.Text = r.PRESSPARTREVISIONNUMBER;

                cbPPARWithoutSample.Checked = p.ISWITHOUTSAMPLE.ToString() == "1" ? true : false;
                txtDeliveredPartsQuantity.Text = p.DELIVERYPQ;
                txtDeliveryCourierCompany.Text = p.DELIVERYCOMPANY;
                txtDeliveryNoteNumber.Text = p.DELIVERYNOTENUMBER;
                dpDeliveryNoteDate1.Text = p.DELIVERYNOTEDATE == null ? "" : p.DELIVERYNOTEDATE == DateTime.Parse("1900-01-01") ? "" : DateTime.Parse(p.DELIVERYNOTEDATE == null ? "1900-01-01" : p.DELIVERYNOTEDATE.ToString()).ToString("yyyy-MM-dd");
            }
            //if (CurrentUser.UnitId == "99")
            //    ddlPlant.Enabled = true;
        }

        public string ShowAuditStatus(string Status, string isinapp)
        {
            string result = "";
            result = "<span style='color:red'>已提交</span>";

            switch (Status.ToLower())
            {
                case "0":
                    if (isinapp == "0")
                        result = "<span style='color:red'>Un-allocated</span>";
                    else
                        result = "<span style='color:blue'>Allocated</span>";
                    break;
                case "1":
                    result = "<span style='color:red'>Created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>Submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>Not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>Circulating</span>";
                    break;
                case "6":
                    result = "<span style='color:blue'>Released</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>Decided</span>";
                    break;

                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            //}
            return result;
        }
    }
}